<?php
if (!defined('INSIDE')) {die('Hacking attempt');}
/**
 * PhlameService
 * 
 * This package is distributed under the
 * GNU General Public License v.3.
 * See the LICENSE.txt file for details.
 * 
 * @package   Phlame
 * @author    Phlegma <phlegma@pheelgood.net>
 * @copyright 2008 by Phlegma, Phlame
 * @link 	  http://ugamela-forum.pheelgood.net
 * @since     29.06.2008
 * @version   0.1
 * @access    public
 * 
 */

class PhlameService
{
	/**
	 * @var Session
	 */
	protected $Session;
	protected $Requesters = array();
	
	protected $class = '';
	protected $data = array();
	protected $data_backup = array();
	protected $write_rights = false;
	
	protected $subclass;
	protected $block = array();
	protected $block_backup = array();
	protected $write_rights_block = false;
	
	protected $select_exec = false;
	
	# Memory / Cache
	protected $data_memory = array();
	protected $data_backups_memory = array();
	protected $write_rights_memory = array();
	protected $blocks_memory = array();
	protected $blocks_backups_memory = array();
	protected $write_rights_block_memory = array();
	
	protected $class_memory = array();
	protected $subclass_memory = array();
	
	# Singleton
	protected static $Instance = NULL;
	/**
	 * PhlameService Contruktor
	 * Returns the unique PhlameService instance
	 * @return PhlameService
	 */
	public static function getInstance()
	{
		if (self::$Instance === NULL)
		{
			self::$Instance = new self();
		}
		return self::$Instance;
	}
	private final function __clone() {}
	
	protected function __construct()
	{
		$this->Session = Session::getInstance();
	}
	
	public function __destruct()
	{
		
	}
	
	public function getByQuery($where, $class, $writeacces = false, $querySub = false)
	{
		$subclasses = array_slice(func_get_args(),2);
		/* @var $Req SubRequester */
		$Req = $this->getRequester((string)$class);
		if ($writeacces)
		{
			$Req->write_acces();
		}
		$ids = $Req->query($where);
		$data = $Req->getLastQueried();
		$subdata = array();
		$subclass = array(-1 => (string)$class); # $i-1 will be the baseclass
		if ($querySub) 
		{
			while ($Req->subAvailable())
			{
				$sa = true;
				$subdata[] = $Req->querySub();
				$subclass[] = $Req->subclass();
			}
		}
		if ($sa) # sub available
		{
			for ($i=count($subdata)-1;$i>=0;$i--) # start nesting by the deepest
			{
				$subm = array(); # flush
				foreach ($subdata[$i] as $sdata)
				{
					$subm[$sdata[strtolower($subclass[$i-1]).'_id']] = # e.g fleet_id
						$this->createModel($subclass[$i], $writeacces, $sdata, isset($csubm[$sdata['id']]) ? $csubm[$sdata['id']] : array()); # get from deeper layer
				}
				$csubm = $subm; #save for higher layer
			}
		}
		
	}
	
	protected function createModel($class, $writeacces, $data, $submodels)
	{
		return new $class($this, TIME, $writeacces, $data, $submodels);
	}
	
	public function getByCall($class, $method, array $params)
	{
		/* @var $Req Requester */
		$Req = $this->getRequester((string)$class);
		$data = call_user_method_array($method, $Req, $params);
	}
	
	protected function getRequester($class)
	{
		return isset($this->requester_memory[$class]) ? $this->requester_memory[$class] : ($this->requester_memory[$class] = call_user_func($class.'_Requester::getInstance'));
	}
	
	public function SelectByID ( $id, $class, $subclass = '')
	{
		$this->select_exec = true;
		$this->class = (string) $class;
		$this->subclass = (string) $subclass;
		$req = isset($this->requester_memory[$this->class]) ? $this->requester_memory[$this->class] : call_user_func(array($this->class.'_Requester','getInstance'));
		$this->data = $req->GetByID($id,$this->class);
		$this->block = $this->subclass ? $req->GetByID($id,$this->subclass) : NULL;
		$this->requester_memory[$this->class] = $req;
	}
	
	public function modelDestruct(Model $model, $data)
	{
		
	}
	
	public function modelSleep(Model $model, $data)
	{
		
	}
	
	public function modelWakeupRefresh(Model $model)
	{
		
	}
}

?>